<?php
// +----------------------------------------------------------------------
// | ZanPianCMS
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpiancms.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.zanpiancms.cc>
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
use think\Cache;
use app\common\library\Letter;
class Star extends Model{
    // 定义时间戳字段名
    protected $createTime = '';
    protected $updateTime = '';
	// 自动完成
	protected $auto = [];
	protected $insert = ['star_addtime'];
	protected $update = [];
    public $xxlist = ['A型','B型','AB型','O型','ABO型','Rh性','Rh阳性','Rh阴性'];
    public $xzlist = ['白羊座','金牛座','双子座','巨蟹座','狮子座','处女座','天秤座','天蝎座','射手座','摩羯座','水瓶座','双鱼座','蛇夫座'];		
	//自动完成相关字段
	protected function setStarAddtimeAttr($value,$data){
		if (!empty($data['star_checktime']) || empty($value)) {
			return time();
		}else{
			return strtotime($value);
		}
	}
	protected function setStarTagAttr($value,$data){
		if(empty($value) && !empty($data['auto_tag'])){
			return zanpian_get_tag($data['star_name'],$data['star_content']);
		}else{
			return $value;
		}
	}
	protected function setStarInfoAttr($value,$data){
		if(!empty($value)) {
			return $value;
		}elseif(!empty($data["star_data_title"]) && !empty($data["star_data_info"])){
		    $title = $data["star_data_title"];
		    $info =  $data["star_data_info"];			
		    foreach($title as $key=>$val){
			    $val=trim($val);
			    if($val){
		            $starinfo[$key]=$val."@@".$info[$key];
			    }
			}
			if(!empty($starinfo)){
			   return implode("||",array_unique($starinfo));
			}else{
				return '';
			}
		}
	}	
	protected function setStarMcidAttr($value,$data){
		if(!empty($value)){
			if(is_array($value)){
				return implode(',', $value);
			}else{
			    return $value;
			}
		}elseif(!empty($data['star_mcid_name'])){
			return model('Mcat')->get_mcid($data['star_cid'],$data['star_mcid_name']);
		}elseif(!empty($data['star_keywords'])){
			return model('Mcat')->get_mcid($data['star_cid'],$data['star_keywords']);
		}else{
			return '';
		}
	}
    protected function setStarPrtyAttr($value){
        if(!is_array($value)){
            return 0;
        }else{
            $attribute = 0;
            foreach ($value as $key=>$value){
                $attribute += $value;
            }
            return $attribute;
        }
    }
	protected function setStarLetterAttr($value,$data){
		$Letter = new Letter();
		if ($data['star_name']) {
			return $Letter->getletter($data['star_name']);
		}
	}
   protected function setStarLettersAttr($value,$data){
	    $Letter = new Letter();
	    if(empty($value)) {
		     return $Letter->getletters(trim($data['star_name']),3);
		}else{
		    return trim($value);
		}
    }
	protected function setStarPicAttr($value){
		$img = model('Img');
		$file=$img->down_load(trim($value),'star');
		return $file['file'];
	}
	protected function setStarPicThumbAttr($value){
		$img = model('Img');
		$file=$img->down_load(trim($value),'star');
		return $file['file'];
	}	
	protected function setStarBigpicAttr($value){
		$img = model('Img');
		$file=$img->down_load(trim($value),'star');
		return $file['file'];
	}
	protected function setStarContentAttr($value){	
	    if(!empty($value)){
	       return zanpian_content_down_images(trim($value),'star');
	    }else{
	       return ''; 	 
	    }
	}
	protected function setStarGoldAttr($value){
		if($value > 10){
			$value = 10;
		}	
		return 	$value;
	}	
	protected function setStarkeywordsAttr($value,$data){
		if(!empty($value)){
			    return $value;
		}elseif(empty($value) &&  !empty($data['star_name']) && !empty($data['auto_keywords'])){
			$get=controller('get');
			$info=$get->keywords($data['star_name'],3);
			if($info['code']=1){
			    return $info['data']['str'];	
			}else{
				return '';
			}
		}else{
			return '';
		}
	}	
	//事件注册
    protected static function init(){
		//写入后置事件
		Star::afterWrite(function ($star){
			if(!empty($star->star_id)){
			   model('Manage')->DelCache(3,$star->star_id);
			}
		});
		//删除后置事件
        Star::afterDelete(function ($star){
			model('topic')->topic_del($star->star_id,3);
			zanpian_del_img_file($star->star_content);
			$upload=config('upload');
			//删除图片和缩略图
            if(!empty($star->star_pic) && file_exists(ROOT_PATH.$star->star_pic)){
                @unlink(ROOT_PATH.$star->star_pic);
			    if (!empty($upload['upload_thumb_size']) && !empty($upload['upload_thumb'])) {
				    $thumbs = explode(',', $upload['upload_thumb_size']);
				    foreach($thumbs as $k => $v){
						$img_path=ROOT_PATH.$star->star_pic.'_'.$v.'.'.strtolower(pathinfo($star->star_pic, PATHINFO_EXTENSION));
                        if(file_exists($img_path)){
                            @unlink($img_path);
                        }					
				    }
			    }				
            }
            if(!empty($star->star_pic_thumb) && file_exists(ROOT_PATH.$star->star_pic_thumb)){
                @unlink(ROOT_PATH.$star->star_pic_thumb);
            }
            if(!empty($star->star_bigpic) && file_exists(ROOT_PATH.$star->star_bigpic)){
                @unlink(ROOT_PATH.$star->star_bigpic);
            }
			model('Manage')->DelCache(3,$star->star_id);
       });	
    }		
   //关联相关表
	public function news(){
        return $this->hasMany('topic','topic_did')->where(['topic_sid'=>3,'topic_tsid'=>2]);
    }
    public function listData($tag){
		$param = zanpiancms_param_url();
		if(!is_array($tag)) {
            $tag=json_decode($tag,true);
        }
		$tag['field']= !empty($tag['field']) ? $tag['field'] : 'star_id,star_cid,star_name,star_bm,star_mcid,star_prty,star_wwm,star_letters,star_tag,star_letter,star_color,star_bgcolor,star_pig,star_pic,star_bigpic,star_xb,star_sg,star_tz,star_mz,star_cstime,star_school,star_gs,star_weibo,star_work,star_guanxi,star_zy,star_xz,star_xx,star_area,star_gj,star_csd,star_content,star_reurl,star_skin,star_jumpurl,star_hits,star_hits_day,star_hits_week,star_hits_month,star_hits_lasttime,star_addtime,star_stars,star_status,star_up,star_down,star_label';
		$tag['limit']= !empty($tag['limit']) ? $tag['limit'] : 10;
		if(!empty($tag['page']) && !empty($param['order'])){
			$tag['order']='star_'.$param['order'].' desc';
		}		
		if(empty(order_check($tag['order']))){
			$tag['order']='star_addtime desc';
		}		
		$cahce=tag_cache($tag);
	    //优先从缓存调用数据及分页变量
		if(!empty($cahce['cache_name']) && !empty($cahce['cache_time'])){			
			$data_cache_content = Cache::get($cahce['cache_name']);
			if($data_cache_content){
				return $data_cache_content;
			}
		}		
		$where=[];
		$where['star_status']=['eq',1];
	    if(!empty($tag['ids'])){
		     $where['star_id'] = array('in',$tag['ids']);
	    }		
	    if(!empty($tag['not'])){
		     $where['star_id'] = array('not in',$tag['not']);
	    }	
	    if (!empty($tag['letter'])) {
		    $letter = explode(',',$tag['letter']);
		    if(count($letter)>1) {
			    $where['star_letter'] = array('in',$tag['letter']);
		    }else{
			    $where['star_letter'] = array('eq',$letter[0]);
		    }
	    }		
	    if(!empty($tag['cid'])) {
		    $cids = explode(',',trim($tag['cid']));
		    if(count($cids) > 1) {
			    $where['star_cid'] = array('in',getlistarr_tag($cids));
		    }else{
			     $where['star_cid'] = getlistsqlin($tag['cid']);
		    }
	    }			
	    if(!empty($tag['name'])) {
		    $name = explode(',',$tag['name']);
		    if(count($name)>1) {
			    $where['star_name'] = array('in',$tag['name']);
		    }else{
			    $where['star_name'] = array('eq',$name[0]);
		    }	
	    }		
	    if(!empty($tag['likename'])) {
		    $where['star_name'] = array('like',$tag['likename'].'%');
	    }		
	    if(!empty($tag['wd'])) {
		    $where['star_name|star_area|star_zy|star_csd|star_work|star_gx|star_content|star_info'] = array('like','%'.$tag['wd'].'%');	
	    }	
        if(!empty($tag['xb'])) {
		    $where['star_xb'] = array('eq',$tag['xb']);
	    }
        if (!empty($tag['sex'])) {
		    $where['star_xb'] = array('eq',$tag['sex']);
	    }		
	    if (!empty($tag['area'])) {
		    $area = explode(',',$tag['area']);
		    if (count($area)>1) {
			    $where['star_area'] = array('in',$tag['area']);
		    }else{
			    $where['star_area'] = array('eq',$tag['area']);
		    }
	    }	
	    if(!empty($tag['zy'])) {
		    $zy = explode(',',$tag['zy']);
		    if (count($zy)>1) {
			    $where['star_zy'] = array('in',$tag['zy']);
		    }else{
			    $where['star_zy'] = array('like','%'.$tag['zy'].'%');
		    }
	    }
	    if(!empty($tag['guanxi'])) {
		    $where['star_guanxi'] = array('like','%'.$tag['guanxi'].'%');
	    }
	    if(!empty($tag['work'])) {
		    $where['star_work'] = array('like','%'.$tag['work'].'%');
	    }				
	    if(!empty($tag['stars'])) {
		     $where['star_stars'] = array('in',$tag['stars']);
	    }	
	    if (!empty($tag['hits'])) {
		    $hits = explode(',',$tag['hits']);
		    if (count($hits) > 1) {
			    $where['star_hits'] = array('between',$hits[0].','.$hits[1]);
		    }else{
			    $where['star_hits'] = array('gt',$hits[0]);
		    }
	    }		
        if(!empty($tag['gold'])) {
		    $gold = explode(',',$tag['gold']);
		    if(count($gold) > 1) {
			    $where['star_gold'] = array('between',$gold[0].','.$gold[1]);
		    }else{
			    $where['star_gold'] = array('gt',$gold[0]);
		    }
	    }		
	    if(!empty($tag['golder'])) {
		    $golder = explode(',',$tag['golder']);
		    if(count($golder) > 1) {
			    $where['star_golder'] = array('between',$golder[0].','.$golder[1]);
		    }else{
			    $where['star_golder'] = array('gt',$golder[0]);
		    }
	    }	
	    if(!empty($tag['up'])) {
		    $up = explode(',',$tag['up']);
		    if(count($up)>1) {
			    $where['star_up'] = array('between',$up[0].','.$up[1]);
		    }else{
			    $where['star_up'] = array('gt',$up[0]);
		    }
	    }		
	    if(!empty($tag['down'])) {
		    $down = explode(',',$tag['down']);
		    if(count($down)>1) {
			    $where['star_down'] = array('between',$down[0].','.$down[1]);
		    }else{
			    $where['star_down'] = array('gt',$down[0]);
		    }
	    }		
        if(!empty($tag['addtime'])){
		    $time = explode(',',$tag['addtime']);
		    if(count($time) > 1) {
			    $where['star_addtime'] = array('between',intval(strtotime($time[0])).','.intval(strtotime($time[1])));
		    }else{
			    $where['star_addtime'] = array('gt',intval(strtotime($time[0])));
		    }			
        }
        if(!empty($tag['star'])){
		    $vodid=db('actors')->where('actors_name',$tag['star'])->column('actors_id');
            $starname=db('actors')->where('actors_id','in',$vodid)->column('actors_name');
			$starname=array_diff(array_unique($starname),[$tag['star']]);
        }
        if(!empty($tag['guan'])){
            $guanxi=@explode(',',$tag['guan']);
			$starname=@array_merge($guanxi,$starname);
        }
        if(!empty($starname)){
			$where['star_name'] = array('in',$starname);
		}		
		if($tag['prty']){
			if(!empty(check_position($tag['prty']))){
			    $where['star_prty'] = check_position($tag['prty']);
			}
		}
        if(!empty($tag['tag'])){
		    $where['tag_name'] = array('eq',$tag['tag']);
		    $where['tag_sid'] = 3;
		    $rs = db('tag')->join('star star','star.star_id = tag_id','RIGHT');
	    }else{
		    $rs=db('star');
		}		
        if(!empty($tag['page'])){
            $url=urldecode(zanpian_list_url($GLOBALS['route'],config('params')));
		    $data = $rs->where($where)->field($tag['field'])->order($tag['order'])->paginate($tag['limit'],false,['path'=>$url]);
		}else{
			$limit_str = intval(abs($tag['start'])).",".intval(abs($tag['limit']));
			$data = $rs->where($where)->field($tag['field'])->order($tag['order'])->limit($limit_str)->select();
		}
		if(!empty($cahce['cache_name']) && !empty($cahce['cache_time'])){
			cache::tag('foreach_star')->set($cahce['cache_name'],$data,intval($cahce['cache_time'])); 
	    }
		return $data;
    } 	
}
// +----------------------------------------------------------------------
// | ZanPianCMS
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpiancms.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.zanpiancms.cc>
// +----------------------------------------------------------------------